package com.syni.mdd.yhd.common.all.utils;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName ParamData
 * @Description 用于直接提取mvc的前端参数
 * @Author cfx
 * @Date 2019/3/8 14:53
 **/
public class ParamData extends HashMap {
    private HttpServletRequest httpServletRequest;
    public ParamData() {
        this.httpServletRequest = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
        Map<String, String[]> propertyMap = this.httpServletRequest.getParameterMap();
        //System.out.println(propertyMap);
        for(Entry property : propertyMap.entrySet()){
            StringBuilder propertyValue = new StringBuilder();
            String[] values = (String[]) property.getValue();
            for(int ii = 0;ii < values.length;ii++){
                propertyValue.append(values[ii] + ",");
            }
            this.put(property.getKey(),propertyValue.toString().substring(0,propertyValue.toString().length() - 1));
        }
    }
    public Object get(Object key){
        if(!this.containsKey(key)){
            return null;
        }
        return super.get(key);
    }

    public String getString(Object key){
        String result = "";
        if(this.containsKey(key) & this.get(key) != null){
            result = this.get(key).toString();
        }
        return result;
    }

    public Integer getInteger(Object key){
        Integer ret = 0;
        if(this.containsKey(key) & this.get(key) != null){
            String value = this.get(key).toString();
            if(!"".equals(value)){
                ret = Integer.parseInt(value);
            }
        }
        return ret;
    }

    public HttpServletRequest getHttpServletRequest() {
        return httpServletRequest;
    }

    public void setHttpServletRequest(HttpServletRequest httpServletRequest) {
        this.httpServletRequest = httpServletRequest;
    }
}
